package org.nearby.note.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;

import org.nearby.note.bean.fastrun;
import org.nearby.note.utils.DbUtil;



public class fastrunDaoImpl implements fastrunDao{
	private Connection conn;
	private PreparedStatement psmt;
	private Statement stmt;
	private ResultSet rs;
	
	@Override
	public Vector<Vector<Object>> getAllFastRun() {
		Vector<Vector<Object>> frs = new Vector<Vector<Object>>();
		conn = DbUtil.getsqliteconnect(true);
		try {
			//stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); //sqlite只能是上述选项
			stmt = conn.createStatement();
			rs = stmt.executeQuery("select * from fastrun");
			
			while(rs.next()){
				Vector<Object> run = new Vector<Object>();
				run.add(rs.getInt(1));
				run.add(rs.getString(2));
				run.add(rs.getString(3));
				run.add(rs.getString(4));
				frs.add(run);
			}
			return frs;
			
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}finally{
			close();
		}
	}
	
	@Override
	public boolean delFastRun(int run_id) {
		conn = DbUtil.getsqliteconnect(true);
		try {
			psmt = conn.prepareStatement("delete from fastrun where run_id = ?");
			psmt.setInt(1, run_id);	
			int resultRow = psmt.executeUpdate();
			return (resultRow != 0);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}finally{
			close();
		}
	}
	
	@Override
	public int addFastRun(fastrun fr) {
		conn = DbUtil.getsqliteconnect(true);
		try {
			psmt = conn.prepareStatement("insert into fastrun (run_name, run_path, lastrun_datetime) values(?, ?, ?)");
			psmt.setString(1, fr.getRun_name());
			psmt.setString(2, fr.getRun_path());
			psmt.setString(3, fr.getLastrun_datetime());
			psmt.executeUpdate();
			
			ResultSet rs = psmt.getGeneratedKeys();
			while(rs.next()){
				return rs.getInt(1);
			}
			return 0;
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}finally{
			close();
		}
	}
	
	public ArrayList<String> getColNames(){
		 ArrayList<String> names = new ArrayList<String>();
		 names.add("编号");
		 names.add("程序名称");
		 names.add("程序路径");
		 names.add("上次执行时间");
		 return names;
	}
	
	public void close() {
		try {
			if (psmt != null) {
				psmt.close();
			}
			if(stmt != null){
				stmt.close();
			}
			if (rs != null) {
				rs.close();
			}
		} catch (SQLException e) {
			Logger.getLogger(noteDaoImpl.class.getName()).log(Level.SEVERE, null, e);
		}
	}

	@Override
	public fastrun getFastRunById(int run_id) {
		conn = DbUtil.getsqliteconnect(true);
		try {
			psmt = conn.prepareStatement("select * from fastrun where run_id = ?");
			psmt.setInt(1, run_id);	
			rs = psmt.executeQuery();
			
			fastrun fr = new fastrun();
			while(rs.next()){
				fr.setRun_id(rs.getInt("run_id"));
				fr.setRun_name(rs.getString("run_name"));
				fr.setRun_path(rs.getString("run_path"));
				fr.setLastrun_datetime(rs.getString("lastrun_datetime"));
				return fr;
			}
			return null;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}finally{
			close();
		}
	}
}
